package com.ssm.controller;

import com.ssm.pojo.Manager;
import com.ssm.pojo.Room;
import com.ssm.pojo.Student;
import com.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    @Qualifier(value = "StudentServiceImpl")
    private StudentService studentService;
    @Autowired
    @Qualifier("managerServiceImpl")
    private ManagerService managerService;
    @Autowired
    @Qualifier("repairServiceImpl")
    private RepairService repairService;
    @Autowired
    @Qualifier("NoticeServiceImpl")
    private NoticeService noticeService;
    @Autowired
    @Qualifier("roomServiceImpl")
    private RoomService roomService;

    @RequestMapping("/check")
    public String check(){
       return "managerLogin";
    }
    @RequestMapping("/login")
    public String login(Integer managerId, String mPassword , HttpSession session, Model model){
        System.out.println(managerId+"密码"+mPassword);
        Manager manager = managerService.queryMangerById(managerId);
        if(manager!=null){
            if(mPassword.equals(manager.getmPassword())){
                session.setAttribute("manager",manager);
                return "manager";
            }
        }
        model.addAttribute("msg","登录失败，请重新登录");
        return "managerLogin";
    }
    @RequestMapping("/queryAllManager")
    public ModelAndView queryAllManager(){
        ModelAndView mv = new ModelAndView();
        List list = managerService.queryAllManger();
        if(list.size()==0){
            mv.addObject("msg","暂无管理员信息");
            mv.setViewName("success");
            return mv;
        }
        mv.addObject("list",list);
        mv.setViewName("manager");
        return mv;
    }
    @RequestMapping("/queryAllRoom")
    public ModelAndView queryAllRoom(){
        ModelAndView mv = new ModelAndView();
        List list = roomService.queryAllRoom();
        if(list.size()==0){
            mv.addObject("msg","没有空床了");
            mv.setViewName("success");
            return mv;
        }
        mv.addObject("rlist",list);
        mv.setViewName("manager");
        return mv;
    }
    @RequestMapping("/queryStudent")
    public String queryStudent(){
        return "queryStudent";
    }
    @RequestMapping("/queryStudentById")
    public ModelAndView queryStudentById(int sId){
        ModelAndView mv = new ModelAndView();
        Student student = studentService.queryStudentBySid(sId);
        if(student==null){
            mv.addObject("msg","你所查的学生不存在");
            mv.setViewName("success");
        }
        mv.addObject("student1",student);
        mv.setViewName("manager");
        return mv;
    }
    @RequestMapping("/queryRepair")
    public String queryRepair(){
        return "queryRepair";
    }
    @RequestMapping("/queryRepairByTime")
    public ModelAndView queryRepairByTime(String rDate){
        ModelAndView mv = new ModelAndView();
        List list = repairService.queryRepair(rDate);
        if(list.size()==0){
            mv.addObject("msg","您查询的时间段没有报修产生");
            mv.setViewName("success");
            return mv;
        }else {
            mv.addObject("repairList",list);
            mv.setViewName("manager");
            return mv;
        }
    }
    @RequestMapping("/updateManger")
    public String updateManger(){
        return "updateManager";
    }
    @RequestMapping("/updateManagerPage")
    public ModelAndView updateManagerPage(HttpSession session,String mPassword,String mPhone,String mName,String mDetail){
        ModelAndView mv = new ModelAndView();
        Manager manager = (Manager) session.getAttribute("manager");
        manager.setmDetail(mDetail);
        manager.setmPhone(mPhone);
        manager.setmName(mName);
        manager.setmPassword(mPassword);
        int i = managerService.updateManager(manager);
        if(i>0){
            mv.addObject("msg","管理员修改成功");
            mv.setViewName("success");
            return mv;
        }
        else {
            mv.setViewName("updateManager");
            return mv;
        }
    }
    @RequestMapping("/transeRoom")
    public  String transeRoom(){
        return "transeStudent";
    }
    @RequestMapping("/transeStudentPage")
    public ModelAndView transeStudentPage(Integer sId,Integer sDormId){
        ModelAndView mv = new ModelAndView();
        Student student = studentService.queryStudentBySid(sId);
        if(student==null){
            mv.addObject("msg","你的学号不存在或没入住");
            mv.setViewName("transeStudent");
        }
        Room room = roomService.queryRoomByID(student.getsDormId());
        student.setsDormId(sDormId);
        int i = studentService.updateStudentBySId(student);
        if(i>0){
            int existNumber = room.getExistNumber();
            if(existNumber>0) {
                room.setExistNumber(existNumber - 1);
                roomService.updateRoomByRoomId(room);
            }
            Room room1 = roomService.queryRoomByID(sDormId);
            if(room1!=null) {
                int existNumber1 = room1.getExistNumber();
                room1.setExistNumber(existNumber1 + 1);
                roomService.updateRoomByRoomId(room1);
            }
            mv.addObject("msg","转宿成功");
            mv.setViewName("success");

        }else {
            mv.addObject("msg","转宿失败重新尝试");
            mv.setViewName("transeStudent");
        }
        return mv;
    }
    @RequestMapping("/addManagerPage")
    public String addManagerPage(){
        return "managerRegist";
    }
    @RequestMapping("/addManager")
    public ModelAndView addManager(Manager manager){
        System.out.println(manager);
        ModelAndView mv = new ModelAndView();
        int i = managerService.addManager(manager);
        if(i>0){
            mv.addObject("msg","管理员信息添加成功");
            mv.setViewName("success");
            return mv;
        }else {
            mv.setViewName("managerRegist");
            return mv;
        }
    }
    @RequestMapping("/deleteManagerPage")
    public String deleteManagerPage(){
        return "deleteManager";
    }
    @RequestMapping("/deleteManagerById")
    public ModelAndView deleteManagerById(Integer managerId){
        ModelAndView mv = new ModelAndView();
        int i = managerService.deleteManager(managerId);
        if(i>0){
            mv.addObject("msg","管理员删除成功");
            mv.setViewName("success");
        }else {
            mv.setViewName("deleteManager");
        }
        return mv;
    }
    @RequestMapping("/addRoom")
    public String addRoom(){
        return "studentRegist";
    }
    @RequestMapping("/addStudentPage")
    public ModelAndView addStudentPage(Student student){
        ModelAndView mv = new ModelAndView();
        Student student1 = studentService.queryStudentBySid(student.getsId());
        if(student1==null) {
            int i = studentService.addStudent(student);
            if(i>0){
                Room room = roomService.queryRoomByID(student.getsDormId());
                System.out.println(room);
                if(room!=null){
                    int existNumber = room.getExistNumber();
                    room.setExistNumber(existNumber+1);
                    roomService.updateRoomByRoomId(room);
                    mv.addObject("msg","入住成功");
                    mv.setViewName("success");
                }else{

                    mv.addObject("msg","该宿舍不存在");
                    mv.setViewName("success");
                }

            }else {
                mv.setViewName("studentRegist");
            }
        }else {
            mv.addObject("msg", "你已入住");
            mv.setViewName("success");
        }
        return mv;
    }
    @RequestMapping("/deleteRoom")
    public  String deleteRoom(){
        return "deleteStudent";
    }
    @RequestMapping("/deleteStudentPage")
    public ModelAndView deleteStudentPage(Integer sId){
        ModelAndView mv = new ModelAndView();
        Student student = studentService.queryStudentBySid(sId);
       if(student==null){
           mv.addObject("msg","你输入学号没有入住");
           mv.setViewName("success");
           return mv;
       }
        int sDormId=student.getsDormId();
        int i = studentService.deleteStudentBySid(sId);
        if(i>0){
            mv.addObject("msg","退宿成功");
            mv.setViewName("success");
            Room room = roomService.queryRoomByID(sDormId);
            System.out.println(room);
            if (room!=null) {
                int existNumber = room.getExistNumber();
                room.setExistNumber(existNumber - 1);
                roomService.updateRoomByRoomId(room);
            }
        }else {
            mv.addObject("msg","退宿失败");
            mv.setViewName("deleteStudent");
        }
        return mv;
    }

}
